package com.genesis.web.controller.member;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.genesis.mall.common.Result;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.MemberBrandAttention;
import com.genesis.mall.service.IMemberBrandAttentionService;
import com.genesis.mall.utils.TokenUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: HShuai
 * @Date: 2022/08/03/18:34星期三
 * @Description: 关注品牌
 */
@Slf4j
@RestController
@RequestMapping("/member/attention")
public class MemberBrandAttentionController {

    @Autowired
    private IMemberBrandAttentionService memberBrandAttentionService;

    /**
     * 添加关注品牌
     *
     * @param memberBrandAttention
     * @return
     */
    @PostMapping("/add")
    public Result save(@RequestBody MemberBrandAttention memberBrandAttention) {

        memberBrandAttentionService.saveBrand(memberBrandAttention);
        return Result.success();

    }

    /**
     * 清空当前用户品牌关注列表
     *
     * @param request
     * @return
     */
    @PostMapping("/clear")
    public Result clear(HttpServletRequest request) {
        memberBrandAttentionService.clear(request);
        return Result.success();
    }

    /**
     * 取消品牌关注
     * @param brandId
     * @return
     */
    @PostMapping("/delete")
    public Result deleteById(Long brandId,HttpServletRequest request) {


        //获取用户id
        String token = request.getHeader("Authorization");
        String token1 = token.substring(6);
        Claims claims = TokenUtil.getClaims(token1);
        Long memberId = Long.parseLong(claims.get("umsMember").toString()) ;
        log.info("用户id:{}",memberId);
        LambdaQueryWrapper<MemberBrandAttention> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberBrandAttention::getBrandId, brandId);
        lqw.eq(MemberBrandAttention::getMemberId,memberId);
        memberBrandAttentionService.remove(lqw);
        return Result.success();
    }

    /**
     * 显示品牌关注详情
     * @param brandId
     * @param request
     * @return
     */
    @GetMapping("/detail")
    public Result<MemberBrandAttention> getDetail (Long brandId,HttpServletRequest request){

        return Result.success(memberBrandAttentionService.getDetailById(brandId,request));

    }

    /**
     * 显示当前用户品牌关注列表
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public Result<PageDto<MemberBrandAttention>> pageDtoResult (HttpServletRequest request,Integer pageNum,Integer pageSize){

        return Result.success(memberBrandAttentionService.pageList(request,pageNum,pageSize));
    }

}
